VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Connect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Attribute VB_Description = "VB Debug Wiper"
Option Explicit
Implements IDTExtensibility

Private VBI As VBIDE.VBE
Private mcbWiperButton As Office.CommandBarControl
Private mcbWiperMenu As Office.CommandBarControl
Private WithEvents WiperButtonHandler As CommandBarEvents
Attribute WiperButtonHandler.VB_VarHelpID = -1
Private WithEvents WiperMenuHandler As CommandBarEvents
Attribute WiperMenuHandler.VB_VarHelpID = -1

Private Sub IDTExtensibility_OnAddInsUpdate(custom() As Variant)
  ' placeholder for this procedure so it doesn't get optimized out during compile
End Sub

Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, ByVal ConnectMode As VBIDE.vbext_ConnectMode, ByVal AddInInst As VBIDE.AddIn, custom() As Variant)
    Set VBI = VBInst
'// Create Toolbar Button On Standard Toolbar
    Set mcbWiperButton = VBI.CommandBars("Standard").Controls.Add(1, , , 20)
    mcbWiperButton.Enabled = False
    Clipboard.SetData LoadResPicture(1000, 0)  '// Put image on Clipboard
    mcbWiperButton.PasteFace
    mcbWiperButton.ToolTipText = "Debug Wiper"
    Set WiperButtonHandler = VBI.Events.CommandBarEvents(mcbWiperButton)
'// Create Menu Item on Add-In's Menu
    Set mcbWiperMenu = VBI.CommandBars("Add-Ins").Controls.Add
    mcbWiperMenu.Enabled = False
    mcbWiperMenu.PasteFace
    mcbWiperMenu.Caption = "Debug &Wiper"
    Set WiperMenuHandler = VBI.Events.CommandBarEvents(mcbWiperMenu)
'// Enable The Wiper Controls
    mcbWiperMenu.Enabled = True
    mcbWiperButton.Enabled = True
End Sub

Private Sub IDTExtensibility_OnDisconnection(ByVal RemoveMode As VBIDE.vbext_DisconnectMode, custom() As Variant)
'// Disconnect Wiper Controls
    mcbWiperButton.Delete
    mcbWiperMenu.Delete
    Set VBI = Nothing
    Set mcbWiperButton = Nothing
    Set mcbWiperMenu = Nothing
End Sub

Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
  ' placeholder for this procedure so it doesn't get optimized out during compile
End Sub

Private Sub WiperButtonHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
'// Clear the Immediate Window By Sending Keystrokes Since We Don't Have a Debug.Clear() method to call
'//
'// Don't use SendKeys("^a") - <CTRL-A> Is Not Handled When No Projects Are Loaded On StartUp
'//
    Dim pWindow As VBIDE.Window
    Set pWindow = VBI.Windows("Immediate")
    If pWindow Is Nothing Then Exit Sub  '// Abort If The Window Is Not Found
    If pWindow.Visible = True Then  '// Skip Clearing The Window If Window Is Not Visible - To Avoid Toggling The Window Just Clear It
      pWindow.SetFocus
      SendKeys ("^({Home})"), True
      SendKeys ("^(+({End}))"), True
      SendKeys ("{Del}"), True
    End If
    Set pWindow = Nothing
End Sub

Private Sub WiperMenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
'// Use WiperButtonHandler_Click() To ReUse Code
    Call WiperButtonHandler_Click(ByVal CommandBarControl, handled, CancelDefault)
End Sub
